package com.xxg.renrenuser.controller;

import com.xxg.renrencommon.domain.result.BaseResult;
import com.xxg.renrenuser.dao.bean.UserBankCardInfo;
import com.xxg.renrenuser.domain.query.BankCardQuery;
import com.xxg.renrenuser.domain.query.UnBindBankCardQuery;
import com.xxg.renrenuser.service.UserBindBankCardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 绑定银行卡controller
 *
 * @author xxg
 * @date 2020/11/6
 */
@Api(tags = "绑定银行卡接口")
@RestController
@RequestMapping("/bankCard")
public class UserBindBankCardController {
    private final UserBindBankCardService userBindBankCardService;

    public UserBindBankCardController(UserBindBankCardService userBindBankCardService) {
        this.userBindBankCardService = userBindBankCardService;
    }

    @ApiOperation("绑定银行卡")
    @RequestMapping(value = "bind", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "bankCardQuery", value = "银行卡信息", required = true, dataType = "BankCardQuery")})
    public BaseResult<Void> bind(@RequestBody @Validated BankCardQuery bankCardQuery, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return userBindBankCardService.bindBankCard(userId, bankCardQuery.getCardNo(), bankCardQuery.getIdNo(), bankCardQuery.getName());
    }

    @ApiOperation("解除绑定银行卡")
    @RequestMapping(value = "unBind", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "unBindBankCardQuery", value = "银行卡信息", required = true, dataType = "UnBindBankCardQuery")})
    public BaseResult<Void> unBind(@RequestBody @Validated UnBindBankCardQuery unBindBankCardQuery, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return userBindBankCardService.unBindBankCard(userId, unBindBankCardQuery.getBindId());
    }

    @ApiOperation("获取绑定银行卡列表")
    @RequestMapping(value = "getBankCards", method = RequestMethod.GET)
    public BaseResult<List<UserBankCardInfo>> getBankCards(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return userBindBankCardService.getBindBankCards(userId);
    }
}
